//
//  ZBasePickerVC.m
//  WeClubs
//
//  Created by Zed Link on 17/5/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZBasePickerVC.h"
#import <Masonry.h>

@interface ZBasePickerVC () <UIPickerViewDelegate, UIPickerViewDataSource>

/** Cover */
@property (weak, nonatomic) UIButton *backgroundBtn;

/** Picker */
@property (weak, nonatomic) UIPickerView *picker;

@end

@implementation ZBasePickerVC

- (void)zLoadSubview
{
    [super zLoadSubview];
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn setAlpha:0.5];
    [btn addTarget:self action:@selector(coverBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.backgroundBtn = btn;
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    UIPickerView *picker = [[UIPickerView alloc] init];
    picker.delegate = self;
    picker.dataSource = self;
    [picker setBackgroundColor:NAVBAR_BG_COLOR];
    
    [self.view addSubview:picker];
    self.picker = picker;
    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(128);
    }];
}

- (void)coverBtnAction:(UIButton *)sender
{
    [self dismissPicker];
}

#pragma mark - Public Method

- (void)presentPicker
{
//    UIWindow *pickerWindow = [[UIWindow alloc] init];
//    pickerWindow.frame = [UIScreen mainScreen].bounds;
//    pickerWindow.windowLevel = UIWindowLevelAlert;
//    
//    [pickerWindow addSubview:self.view];
//    [KEY_WINDOW addSubview:pickerWindow];
    
    [KEY_WINDOW addSubview:self.view];
}

- (void)dismissPicker
{
    [self.view removeFromSuperview];
}

#pragma mark - UIPickerViewDelegate & UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.doubleCoupleSource != nil) {
        return self.doubleCoupleSource.count;
    } else if (self.singleCoupleSource != nil) {
        return 1;
    }
    return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.doubleCoupleSource != nil) {
        NSArray *source = self.doubleCoupleSource[component];
        return source.count;
    }
    
    if (self.singleCoupleSource != nil) {
        return self.singleCoupleSource.count;
    }
    
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (self.doubleCoupleSource != nil) {
        NSArray *source = self.doubleCoupleSource[component];
        NSString *title = source[row];
        return title;
    }
    
    if (self.singleCoupleSource != nil) {
        NSString *title = self.singleCoupleSource[row];
        return title;
    }
    
    return nil;
}

@end
